library(knitr)

#Data Subset for Each PID X Variable Combinations
bes22rdd_con <- bes_w22 |> filter(partyId==1)
bes22rdd_lab <- bes_w22 |> filter(partyId==2)
bes22rdd_ind <- bes_w22 |> filter(partyId==10)

# Restrict outcomes to their original response scales to drop invalid/missing responses
bes22rdd_all_covid <- bes_w22 |> 
  filter(handleCorona<=5)
bes22rdd_con_covid <- bes22rdd_con |> 
  filter(handleCorona<=5)
bes22rdd_lab_covid <- bes22rdd_lab |> 
  filter(handleCorona<=5)
bes22rdd_ind_covid <- bes22rdd_ind |> 
  filter(handleCorona<=5)

bes22rdd_all_lockdown <- bes_w22 |> 
  filter(govtHandlelockdown<=5)
bes22rdd_con_lockdown <- bes22rdd_con |> 
  filter(govtHandlelockdown<=5)
bes22rdd_lab_lockdown <- bes22rdd_lab |> 
  filter(govtHandlelockdown<=5)
bes22rdd_ind_lockdown <- bes22rdd_ind|> 
  filter(govtHandlelockdown<=5)

bes22rdd_all_likecon <- bes_w22 |> 
  filter(likeCon<=10)
bes22rdd_con_likecon <- bes22rdd_con |> 
  filter(likeCon<=10)
bes22rdd_lab_likecon <- bes22rdd_lab |> 
  filter(likeCon<=10)
bes22rdd_ind_likecon <- bes22rdd_ind |> 
  filter(likeCon<=10)


bes22rdd_all_likejohnson <- bes_w22 |> 
  filter(likeJohnson<=10)
bes22rdd_con_likejohnson <- bes22rdd_con |> 
  filter(likeJohnson<=10)
bes22rdd_lab_likejohnson <- bes22rdd_lab |> 
  filter(likeJohnson<=10)
bes22rdd_ind_likejohnson <- bes22rdd_ind |> 
  filter(likeJohnson<=10)

#Run RDiT Models
#COVID Variables
rdd_con_lockdown <- rdrobust(bes22rdd_con_lockdown$govtHandlelockdown,
                             bes22rdd_con_lockdown$date_diff,h=7,p=1) 
rdd_lab_lockdown <- rdrobust(bes22rdd_lab_lockdown$govtHandlelockdown,
                             bes22rdd_lab_lockdown$date_diff,h=7,p=1) 
rdd_ind_lockdown <- rdrobust(bes22rdd_ind_lockdown$govtHandlelockdown,
                             bes22rdd_ind_lockdown$date_diff,h=7,p=1) 

rdd_con_covid <- rdrobust(bes22rdd_con_covid$handleCorona,
                          bes22rdd_con_covid$date_diff,h=7,p=1) 
rdd_lab_covid <- rdrobust(bes22rdd_lab_covid$handleCorona,
                          bes22rdd_lab_covid$date_diff,h=7,p=1) 
rdd_ind_covid <- rdrobust(bes22rdd_ind_covid$handleCorona,
                          bes22rdd_ind_covid$date_diff,h=7,p=1) 


#Favorability toward the Conservative Party
rdd_con_likecon <- rdrobust(bes22rdd_con_likecon$likeCon,
                            bes22rdd_con_likecon$date_diff,h=7,p=1) 
rdd_lab_likecon <- rdrobust(bes22rdd_lab_likecon$likeCon,
                            bes22rdd_lab_likecon$date_diff,h=7,p=1) 
rdd_ind_likecon <- rdrobust(bes22rdd_ind_likecon$likeCon,
                            bes22rdd_ind_likecon$date_diff,h=7,p=1) 

rdd_con_likejohnson <- rdrobust(bes22rdd_con_likejohnson$likeJohnson,
                                bes22rdd_con_likejohnson$date_diff,h=7,p=1) 
rdd_lab_likejohnson <- rdrobust(bes22rdd_lab_likejohnson$likeJohnson,
                                bes22rdd_lab_likejohnson$date_diff,h=7,p=1) 
rdd_ind_likejohnson <- rdrobust(bes22rdd_ind_likejohnson$likeJohnson,
                                bes22rdd_ind_likejohnson$date_diff,h=7,p=1) 

#Vote Intention
rdd_con_convote <- rdrobust(bes22rdd_con$convote,
                            bes22rdd_con$date_diff,h=7,p=1) 
rdd_lab_convote <- rdrobust(bes22rdd_lab$convote,
                            bes22rdd_lab$date_diff,h=7,p=1) 
rdd_ind_convote <- rdrobust(bes22rdd_ind$convote,
                            bes22rdd_ind$date_diff,h=7,p=1) 

rdd_con_labvote <- rdrobust(bes22rdd_con$labvote,
                            bes22rdd_con$date_diff,h=7,p=1) 
rdd_lab_labvote <- rdrobust(bes22rdd_lab$labvote,
                            bes22rdd_lab$date_diff,h=7,p=1) 
rdd_ind_labvote <- rdrobust(bes22rdd_ind$labvote,
                            bes22rdd_ind$date_diff,h=7,p=1) 

#Datasets that contain coefficients and standard errors
rdd_df_covid <- tibble(
  Coef=NA,Std=NA,DV=rep(c("COVID-19 Performance (1-5)","Lockdown Performance (1-5)"),3),
  Party=c("Cons","Cons","Lab","Lab","Ind","Ind")
)

rdd_df_like <- tibble(
  Coef=NA,Std=NA,DV=rep(c("Like Cons (0-10)","Like Johnson (0-10)"),3),
  Party=c("Cons","Cons","Lab","Lab","Ind","Ind")
)

rdd_df_vote <- tibble(
  Coef=NA,Std=NA,DV=rep(c("Vote Intention: Con (0-1)","Vote Intention: Lab (0-1)"),3),
  Party=c("Cons","Cons","Lab","Lab","Ind","Ind")
)

#Store the coefficients and standard errors
rdd_df_covid$Coef[1] <- rdd_con_covid$Estimate[1,1]
rdd_df_covid$Coef[2] <- rdd_con_lockdown$Estimate[1,1]
rdd_df_covid$Coef[3] <- rdd_lab_covid$Estimate[1,1]
rdd_df_covid$Coef[4] <- rdd_lab_lockdown$Estimate[1,1]
rdd_df_covid$Coef[5] <- rdd_ind_covid$Estimate[1,1]
rdd_df_covid$Coef[6] <- rdd_ind_lockdown$Estimate[1,1]

rdd_df_covid$Std[1] <- rdd_con_covid$Estimate[1,3]
rdd_df_covid$Std[2] <- rdd_con_lockdown$Estimate[1,3]
rdd_df_covid$Std[3] <- rdd_lab_covid$Estimate[1,3]
rdd_df_covid$Std[4] <- rdd_lab_lockdown$Estimate[1,3]
rdd_df_covid$Std[5] <- rdd_ind_covid$Estimate[1,3]
rdd_df_covid$Std[6] <- rdd_ind_lockdown$Estimate[1,3]

rdd_df_like$Coef[1] <- rdd_con_likecon$Estimate[1,1]
rdd_df_like$Coef[2] <- rdd_con_likejohnson$Estimate[1,1]
rdd_df_like$Coef[3] <- rdd_lab_likecon$Estimate[1,1]
rdd_df_like$Coef[4] <- rdd_lab_likejohnson$Estimate[1,1]
rdd_df_like$Coef[5] <- rdd_ind_likecon$Estimate[1,1]
rdd_df_like$Coef[6] <- rdd_ind_likejohnson$Estimate[1,1]

rdd_df_like$Std[1] <- rdd_con_likecon$Estimate[1,3]
rdd_df_like$Std[2] <- rdd_con_likejohnson$Estimate[1,3]
rdd_df_like$Std[3] <- rdd_lab_likecon$Estimate[1,3]
rdd_df_like$Std[4] <- rdd_lab_likejohnson$Estimate[1,3]
rdd_df_like$Std[5] <- rdd_ind_likecon$Estimate[1,3]
rdd_df_like$Std[6] <- rdd_ind_likejohnson$Estimate[1,3]

rdd_df_vote$Coef[1] <- rdd_con_convote$Estimate[1,1]
rdd_df_vote$Coef[2] <- rdd_con_labvote$Estimate[1,1]
rdd_df_vote$Coef[3] <- rdd_lab_convote$Estimate[1,1]
rdd_df_vote$Coef[4] <- rdd_lab_labvote$Estimate[1,1]
rdd_df_vote$Coef[5] <- rdd_ind_convote$Estimate[1,1]
rdd_df_vote$Coef[6] <- rdd_ind_labvote$Estimate[1,1]

rdd_df_vote$Std[1] <- rdd_con_convote$Estimate[1,3]
rdd_df_vote$Std[2] <- rdd_con_labvote$Estimate[1,3]
rdd_df_vote$Std[3] <- rdd_lab_convote$Estimate[1,3]
rdd_df_vote$Std[4] <- rdd_lab_labvote$Estimate[1,3]
rdd_df_vote$Std[5] <- rdd_ind_convote$Estimate[1,3]
rdd_df_vote$Std[6] <- rdd_ind_labvote$Estimate[1,3]

#Calculate 95% CI
rdd_df_covid <- rdd_df_covid |> 
  mutate(
    ll_95 = Coef - 1.96 * Std,
    ul_95 = Coef + 1.96 * Std,
    Party_order = factor(Party, levels = c("Cons", "Lab", "Ind"))
  )

rdd_df_like <- rdd_df_like |> 
  mutate(
    ll_95 = Coef - 1.96 * Std,
    ul_95 = Coef + 1.96 * Std,
    Party_order = factor(Party, levels = c("Cons", "Lab", "Ind"))
  )

rdd_df_vote <- rdd_df_vote |> 
  mutate(
    ll_95 = Coef - 1.96 * Std,
    ul_95 = Coef + 1.96 * Std,
    Party_order = factor(Party, levels = c("Cons", "Lab", "Ind"))
  )

#Tables for latex
#Table K1
kable(rdd_df_covid, format = "latex", booktabs = TRUE,
      caption = "RDiT estimates (h = 7, p = 1)",
      label = "tab:rdit_rdrobust",digits = 3)

#Table K2
kable(rdd_df_like, format = "latex", booktabs = TRUE,
      caption = "RDiT estimates (h = 7, p = 1)",
      label = "tab:rdit_rdrobust",digits = 3)

#Table K3
kable(rdd_df_vote, format = "latex", booktabs = TRUE,
      caption = "RDiT estimates (h = 7, p = 1)",
      label = "tab:rdit_rdrobust",digits = 3)

